﻿
using System;
using CachingTest;
using Acidaes.Caching;
using System.Configuration;


public partial class UpdateOrder : System.Web.UI.Page
{
    

  protected void Page_Load(object sender, EventArgs e)
  {

    OrderId.Enabled = true;
    OrderDesc.Enabled = false;
    OrderAmt.Enabled = false;
    OrderQuant.Enabled = false;
    UpdateButton.Enabled = false;
  }

  protected void GetOrder_Click(object sender, EventArgs e)
  {
    Order order = null;
    
    try
    {
        order = (Order)(new CacheClient().Get(OrderId.Text));
      if (order == null)
      {
        UpdateResult.Text = "Order " + OrderId.Text + " not found";
      }
      else
      {
        OrderDesc.Text = order.Description;
        OrderDesc.Enabled = true;
        OrderAmt.Text = order.Amount;
        OrderAmt.Enabled = true;
        OrderQuant.Text = order.Quantity.ToString();
        OrderQuant.Enabled = true;
        UpdateButton.Enabled = true;
        OrderId.Enabled = false;
        UpdateResult.Text = "";
      }
    }
    catch (Exception e1)
    {
      UpdateResult.Text = "Exception " + e1.Message;
    }
  }

  protected void UpdateOrder_Click(object sender, EventArgs e)
  {
    Order order = null;
    //CacheItemVersion version = null;

    try
    {
        order = (Order) (new CacheClient().Get(OrderId.Text));
      if (order == null)
      {
        UpdateResult.Text = "Order " + OrderId.Text + " not found";
      }
      else
      {
        order.Description = OrderDesc.Text;
        order.Amount = OrderAmt.Text;
        order.Quantity = Convert.ToInt32(OrderQuant.Text);
      }
    }
    catch (Exception e1)
    {
        UpdateResult.Text = "Invalid Order: " + e1.Message;
    }

    try
    {
        new CacheClient().Put(OrderId.Text, (object)order);
        UpdateResult.Text = "Order " + OrderId.Text + " successfully updated";
    }
    catch (Exception e2)
    {
        UpdateResult.Text = "Update Failed: " + e2.Message;
    }    
  }
}
